var userName;//用户名
var password;//密码
var confirmPassword;//确认密码
var validateCode;//验证码
var XmlHttpRequ;
window.onload = function(){

    createCode();
    document.getElementById("txtUserName").onfocus = function(){
        document.getElementById("showUserName").innerHTML = "*用户名只能由长度为5-16个字母数字下划线组成";
    }
    document.getElementById("txtPassword").onfocus = function(){
        document.getElementById("showPassword").innerHTML = "密码只能由6-15个字母、数字、下划线  组成！";
    }
    document.getElementById("txtConfirmPassword").onfocus = function(){
        document.getElementById("showConfirmPassword").innerHTML = "确认密码必须和密码一致！";
    }
    document.getElementById("txtUserName").onblur = function(){
        checkUserName();
    }
    
    
    
    //检查密码
    document.getElementById("txtPassword").onblur = function(){
        checkPassword();
    }
    
    //检查确认密码
    document.getElementById("txtConfirmPassword").onblur = function(){
        checkConfirmPassword();
    } 
    document.getElementById("txtValidateCode").onblur = function(){
        checkCode();
        
    }
	
	//点击图片
	document.getElementById("randImage").onclick = function(){
        loadimage();
    }

	document.getElementById("changeImage").click= function(){
		loadimage();
	}

    
    //检查用户是否存在
    document.getElementById("checkName").onclick = function(){
		alert("checkUseExists");
        checkUseExists();
    }
   
    
}
//加载验证码
function createCode(){
    document.getElementById("randImage").src = "image.jsp?" + Math.random();
    document.getElementById("showCheckCode").innerHTML = "请输入图片上的数字";
    
}

//检查用户名
function checkUserName(){
    var patrn = /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,17}$/;
    userName = document.getElementById("txtUserName").value;
    if ((userName.length == 0) || (userName == "")) {
        document.getElementById("showUserName").innerHTML = "*用户不能为空";
        return false;
    }
    else 
        if (!patrn.exec(userName)) {
            document.getElementById("showUserName").innerHTML = "只能输入5-16个以字母开头、可带数字、“_”、“.”的字符组成 ";
            return false;
        }
        else {
            document.getElementById("showUserName").innerHTML = "";
            return true;
        }
    
}

//检查用户是否存在
function checkUseExists(){
    XmlHttpRequ = createXmlHttpRequest();
    
    XmlHttpRequ.onreadystatechange = function(){
        if (XmlHttpRequ.readyState == 4) {
            if (XmlHttpRequ.status == 200 || XmlHttpRequ.status == 304) {
                var data = XmlHttpRequ.resposeText;
                document.getElementById("checkName").innerHtml = date;
                if (data == "用户名已存在") {
                    return false;
                }
                return true;
            }
        }
    }
    XmlHttpRequ.open("post", "text-result", true);
    XmlHttpRequ.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    XmlHttpRequ.send("userName=" + document.getElementById("userName").value);
}

function checkPassword(){
    var patrn = /^(\w){6,15}$/;
    password = document.getElementById("txtPassword").value;
    if ((password.length == 0) || (password == "")) {
        document.getElementById("showPassword").innerHTML = "密码不能为空！";
        return false;
        
    }
    else 
        if (!patrn.exec(password)) {
            document.getElementById("showPassword").innerHTML = "只能由6-15个字母、数字、下划线  组成 ";
            return false;
        }
        else {
            document.getElementById("showPassword").innerHTML = "";
            return true;
        }
}

//检查确认密码
function checkConfirmPassword(){
    var patrn = /^(\w){6,15}$/;
    confirmPassword = document.getElementById("txtConfirmPassword").value;
    if ((confirmPassword.length == 0) || (confirmPassword == "")) {
        document.getElementById("showConfirmPassword").innerHTML = "*确认密码不能为空！";
        return false;
    }
    else 
        if (!patrn.exec(confirmPassword)) {
            document.getElementById("showConfirmPassword").innerHTML = "只能由6-15个字母、数字、下划线  组成 ";
            return false;
        }
        else 
            if (password != confirmPassword) {
                document.getElementById("showConfirmPassword").innerHTML = "*两次密码不一致！";
                return false;
            }
            else {
                document.getElementById("showConfirmPassword").innerHTML = "";
                return true;
            }
    
}

//创建xmlHttpRequest
function createXmlHttpRequest(xmlHttpReq){
    var xmlHttpReq;
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    }
    else 
        if (window.ActiveXObject) {
            try {
                XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
            } 
            catch (e) {
                try {
                    XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
                } 
                catch (e) {
                }
            }
        }
    return xmlHttpReq;
}

function checkCode(){
   validateCode = "<%=session.getAttribute('rand')%>";
   alert(document.getElementById("txtValidateCode").value);
    if (validateCode == document.getElementById("txtValidateCode").value) {
        document.getElementById("showCheckCode").innerHTML = "";
        return true;
    }
    document.getElementById("showCheckCode").innerHTML = "验证码错误！";
	createCode();
    return false;
}

function validateForm(){
    if (checkUserName() && checkPassword() && checkConfirmPassword() && checkUseExists() && checkCode()) {
        return true;
    }
    return false;
}


